Skip to content

EffOperations #7764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: dev/feature
Choose a base branch
from

Conversation

Absolutionism
Copy link
Contributor

@Absolutionism Absolutionism commented Apr 1, 2025

Description

This PR aims to add effects for multiplication, division and exponentiation operations, allowing users to perform an arithmetic operation on an variable, single or list. Rather than doing, for example set {_int} to {_int} * 2
Does not work for literals.


Target Minecraft Versions: any
Requirements: none
Related Issues: #7763

@Absolutionism Absolutionism requested a review from a team as a code owner April 1, 2025 21:14
@Absolutionism Absolutionism requested review from APickledWalrus and Efnilite and removed request for a team April 1, 2025 21:14
Copy link
Member

@sovdeeth sovdeeth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will look more closely later

@sovdeeth sovdeeth added the feature Pull request adding a new feature. label Apr 4, 2025
@Absolutionism Absolutionism requested a review from sovdeeth April 10, 2025 11:04
Copy link
Member

@sovdeeth sovdeeth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

everything else looks good, though there might be an opportunity for optimization if the left expression has a return value that exactly matches an operation, then you wouldn't have to do lookups during the function. Optional, though.

Copy link
Member

@APickledWalrus APickledWalrus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are some initial things I noticed


test "operations effect invalid operation": # TODO: test for runtime errors
set {_date} to now
multiply {_date} by 2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be asserting the value of {_date} afterwards

Comment on lines 164 to 170
error("Cannot operate with a null object.");
return;
}
if (left.isSingle() && left.getSingle(event) == null) {
error("Cannot operate on a null object.");
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be doing errors here imo. Precedent is for effects acting on nothing to simply do nothing.

Copy link
Member

@APickledWalrus APickledWalrus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might like to see us experiment with having this piggyback off of ExprArithmetic. That is, you create a new ExprArithmetic and initialize it with the associated matched pattern and such. You can then just evaluate the expression in execute and change the value. If there is some major reason this wouldn't (or doesn't) work, let me know. OR, it might be better to just pull out some of the shared logic for unparsed literal/return type handling

Also, it might not hurt to have an arithmetic module :) would like to hear what others think about that though (would also provide organization for my proposal above)

divide 10 by {_num}
""")
@Since("INSERT VERSION")
public class EffOperations extends Effect implements SyntaxRuntimeErrorProducer {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not EffArithmetic?

@sovdeeth sovdeeth moved this to In Progress in 2.12 Release May 14, 2025
@skriptlang-automation skriptlang-automation bot added needs reviews A PR that needs additional reviews and removed needs reviews A PR that needs additional reviews labels May 15, 2025
@sovdeeth sovdeeth moved this from In Progress to Needs Reviews in 2.12 Release Jun 3, 2025
Copy link
Member

@APickledWalrus APickledWalrus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking pretty good. I'm still wondering if we need to mimic the UnparsedLiteral logic from ExprArithmetic, but I'll get back to you on that.

multiply {_timespan} by 3
""")
@Example("""
# Will error due to literal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to include invalid examples

return node;
}

private void printArithmeticError(Class<?> left, Class<?> right) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would mark these methods as static if possible

Operator operator,
Class<L> leftClass,
Class<R> rightClass,
Class<?>... possibleReturnTypes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might call it allowedReturnTypes

Comment on lines +162 to +170
Object rightObject = right.getSingle(event);
if (rightObject == null) {
error("Cannot operate with a null object.");
return;
}
if (left.isSingle() && left.getSingle(event) == null) {
error("Cannot operate on a null object.");
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we'd want runtime errors or not for null values (would like to hear what others think). Some operations/types have default values... do we need to be checking those here?

@APickledWalrus APickledWalrus linked an issue Jun 4, 2025 that may be closed by this pull request
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Pull request adding a new feature.
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

Operation Effects
5 participants